'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class TlvUserIconIdAndHash
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1D)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + 1 + 1 + _IconMD5Hash.Count
    End Function

    Private _IconId As Integer
    Public Property IconId() As Integer
        Get
            Return _IconId
        End Get
        Set(ByVal value As Integer)
            _IconId = value
        End Set
    End Property

    Private _IconFlags As Byte
    Public Property IconFlags() As Byte
        Get
            Return _IconFlags
        End Get
        Set(ByVal value As Byte)
            _IconFlags = value
        End Set
    End Property

    Private _IconHashLength As Byte
    Public Property IconHashLenght() As Byte
        Get
            Return _IconHashLength
        End Get
        Set(ByVal value As Byte)
            _IconHashLength = value
        End Set
    End Property

    Private _IconMD5Hash As New List(Of Byte)
    Public ReadOnly Property IconMD5Hash() As List(Of Byte)
        Get
            Return _IconMD5Hash
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _IconId = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        _IconFlags = data(index)
        index += 1

        _IconHashLength = data(index)
        index += 1

        _IconMD5Hash.AddRange(data.GetRange(index, _IconHashLength))
        index += _IconMD5Hash.Count
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_IconId)))
        data.Add(_IconFlags)
        data.Add(_IconHashLength)
        data.AddRange(_IconMD5Hash)

        Return data
    End Function
End Class